package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.EqpEntoty;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.RecipeListEntity;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class RecipeTEDownloadActivity extends BaseDialogActivity {


    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.tvRecipeName)
    TextView tvRecipeName;
    @BindView(R.id.tvRecipeVersion)
    TextView tvRecipeVersion;
    @BindView(R.id.tvRecipeType)
    TextView tvRecipeType;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvAttribute1)
    TextView tvAttribute1;
    @BindView(R.id.tvPartName)
    TextView tvPartName;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_download_te;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("TE下载Recipe");
        freeUI_titleView.setRightListener("下载", new View.OnClickListener() {
            @Override
            public void onClick(View s) {
                if (etEqpId.getText().toString().equals("")){
                    showToast("请扫描设备ID",false);
                    return;
                }
                if (tvRecipeName.getText().toString().equals("")){
                    showToast("没有查询到产品Recipe名称",false);
                    return;
                }
                if (etLotId.getText().toString().equals("")){
                    showToast("请扫描批次ID",false);
                    return;
                }
                DownloadRecipeById();
            }
        });

    }


    @Override
    protected void initData() {

        etEqpId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    requestEqpInfo(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        initEdit(etLotId,s ->{
            if (tvEqpType.getText().toString().equals("") && etEqpId.getText().toString().equals("")){
                etEqpId.requestFocus();
                showToast("请先扫描设备",false);
                return;
            }

            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("LotID", s);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            NetUtils.requestPostUtils("getLotInfo","GetLotInfo",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<LotBean>>(
                    new TypeToken<ResponseBean<LotBean>>() {
                    }.getType(), getSupportFragmentManager(), this) {
                @Override
                public void onSuccess(Response<ResponseBean<LotBean>> response) {
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        tvStep.setText(response.body().getBody().getLotInfo().getSTEPDESC());
                        tvAttribute1.setText(response.body().getBody().getLotInfo().getATTRIBUTE1());
                        tvPartName.setText(response.body().getBody().getLotInfo().getPARTNAME());
                        getRecipeInfo(s);
                    }
                }

                @Override
                public void onError(Response<ResponseBean<LotBean>> response) {
                    super.onError(response);
                    loadError(response.getException(), "GetLotInfo");
                }
            });
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void requestEqpInfo(final String eqpID) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("getEqpInfo","GetEqpInfo",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<EqpEntoty>>(
                new TypeToken<ResponseBean<EqpEntoty>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<EqpEntoty>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    tvEqpType.setText(response.body().getBody().getEqpType());
                }else {
                    tvEqpType.setText("");
                }
            }

            @Override
            public void onError(Response<ResponseBean<EqpEntoty>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEqpInfo");
            }
        });
    }

    private void getRecipeInfo(String lotID) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", etEqpId.getText().toString());
            jsonObject.put("LotID", lotID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("getRecipeInfo","GetRecipeInfo",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<RecipeListEntity>>(
                new TypeToken<ResponseBean<RecipeListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<RecipeListEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    tvRecipeName.setText(response.body().getBody().getRecipeInfo().getRecipeName());
                    tvRecipeVersion.setText(response.body().getBody().getRecipeInfo().getRecipeVersion());
                    tvRecipeType.setText(response.body().getBody().getRecipeInfo().getRecipeType());
                }else {
                    tvRecipeName.setText("");
                    tvRecipeVersion.setText("");
                    tvRecipeType.setText("");
                }
            }

            @Override
            public void onError(Response<ResponseBean<RecipeListEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "GetRecipeInfo");
            }
        });
    }

    private void DownloadRecipeById() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", etEqpId.getText().toString());
            jsonObject.put("RecipeName", tvRecipeName.getText().toString());
            jsonObject.put("RecipeVer",tvRecipeVersion.getText().toString());
            jsonObject.put("ProductName",tvPartName.getText().toString());
            jsonObject.put("LotId",etLotId.getText().toString());
            jsonObject.put("PackageType",tvAttribute1.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("downloadRecipeByName","DownloadRecipeByName",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<Object>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("Recipe下载成功",true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    },2000);
                }
            }

            @Override
            public void onError(Response<ResponseBean<Object>> response) {
                super.onError(response);
                loadError(response.getException(), "DownloadRecipeByName");
            }
        });
    }

}
